package co.yixiang.hw.service.impl;

import co.yixiang.hw.domain.SupplierGoodsSkuLog;
import co.yixiang.hw.repository.SupplierGoodsSkuLogRepository;
import co.yixiang.hw.service.SupplierGoodsSkuLogService;
import co.yixiang.hw.service.dto.SupplierGoodsSkuLogDto;
import co.yixiang.hw.service.dto.SupplierGoodsSkuLogQueryCriteria;
import co.yixiang.hw.service.mapper.SupplierGoodsSkuLogMapper;
import co.yixiang.utils.FileUtil;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import co.yixiang.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;


/**
* @author Walert
* @date 2021-03-27
*/
@Service
//@CacheConfig(cacheNames = "supplierGoodsSkuLog")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SupplierGoodsSkuLogServiceImpl implements SupplierGoodsSkuLogService {

    @Resource
    private SupplierGoodsSkuLogRepository supplierGoodsSkuLogRepository;
    @Resource
    private SupplierGoodsSkuLogMapper supplierGoodsSkuLogMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(SupplierGoodsSkuLogQueryCriteria criteria, Pageable pageable){
        Page<SupplierGoodsSkuLog> page = supplierGoodsSkuLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(supplierGoodsSkuLogMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<SupplierGoodsSkuLogDto> queryAll(SupplierGoodsSkuLogQueryCriteria criteria){
        return supplierGoodsSkuLogMapper.toDto(supplierGoodsSkuLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public SupplierGoodsSkuLogDto findById(Long id) {
        SupplierGoodsSkuLog supplierGoodsSkuLog = supplierGoodsSkuLogRepository.findById(id).orElseGet(SupplierGoodsSkuLog::new);
        ValidationUtil.isNull(supplierGoodsSkuLog.getId(),"SupplierGoodsSkuLog","id",id);
        return supplierGoodsSkuLogMapper.toDto(supplierGoodsSkuLog);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public SupplierGoodsSkuLogDto create(SupplierGoodsSkuLog resources) {
        return supplierGoodsSkuLogMapper.toDto(supplierGoodsSkuLogRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(SupplierGoodsSkuLog resources) {
        SupplierGoodsSkuLog supplierGoodsSkuLog = supplierGoodsSkuLogRepository.findById(resources.getId()).orElseGet(SupplierGoodsSkuLog::new);
        ValidationUtil.isNull( supplierGoodsSkuLog.getId(),"SupplierGoodsSkuLog","id",resources.getId());
        supplierGoodsSkuLog.copy(resources);
        supplierGoodsSkuLogRepository.save(supplierGoodsSkuLog);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            supplierGoodsSkuLogRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<SupplierGoodsSkuLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SupplierGoodsSkuLogDto supplierGoodsSkuLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("商品SKU_ID", supplierGoodsSkuLog.getSkuId());
            map.put("变更时间", supplierGoodsSkuLog.getChangeTime());
            map.put("上市时间", supplierGoodsSkuLog.getAddTime());
            map.put("退市时间", supplierGoodsSkuLog.getOutTime());
            map.put("价格有效时间", supplierGoodsSkuLog.getEffeTime());
            map.put("一级供货价(元)", supplierGoodsSkuLog.getFirstSourcePrice());
            map.put("一级售价(元)", supplierGoodsSkuLog.getFirstSalePrice());
            map.put("二级供货价(元)", supplierGoodsSkuLog.getSecondSourcePrice());
            map.put("二级售价(元)", supplierGoodsSkuLog.getSecondSalePrice());
            map.put("三级供货价(元)", supplierGoodsSkuLog.getThridSourcePrice());
            map.put("三级售价(元)", supplierGoodsSkuLog.getThridSalePrice());
            map.put("零售供货价(元)", supplierGoodsSkuLog.getSourcePrice());
            map.put("零售售价(元)", supplierGoodsSkuLog.getSalePrice());
            map.put("是否删除 0：否 1：是", supplierGoodsSkuLog.getIsDel());
            map.put("创建时间", supplierGoodsSkuLog.getCreateTime());
            map.put("创建人", supplierGoodsSkuLog.getCreateUser());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public List<Map<String, Long>> getSkuDaysPrice(Long skuId,Integer level){
        List<Map<String, Long>> result = new ArrayList<>();
        List<Map<String, BigInteger>> tmpResult = supplierGoodsSkuLogRepository.getPricesBySkuId(skuId);
        for (Map<String, BigInteger> stringLongMap : tmpResult) {
            Map<String, Long> map = new HashMap<>();
            BigInteger changeTime = stringLongMap.get("changeTime");
            map.put("date",changeTime.longValue());
            Long salePrice = 0L;
            Long sourcePrice = 0L;
            if(level == 0){
                salePrice = stringLongMap.get("salePrice").longValue();
            }else if(level == 1){
                salePrice = stringLongMap.get("firstSalePrice").longValue();
                sourcePrice = stringLongMap.get("firstSourcePrice").longValue();
            }else if(level == 2){
                salePrice = stringLongMap.get("secondSalePrice").longValue();
                sourcePrice = stringLongMap.get("secondSourcePrice").longValue();
            }else if(level == 3){
                salePrice = stringLongMap.get("thridSalePrice").longValue();
                sourcePrice = stringLongMap.get("thridSourcePrice").longValue();
            }else if(level == 4){
                salePrice = stringLongMap.get("salePrice").longValue();
                sourcePrice = stringLongMap.get("sourcePrice").longValue();
            }
            map.put("stepAccount",salePrice/100);
            map.put("sourcePrice",sourcePrice/100);
            result.add(map);
        }
        return result;
    }
}
